cmake_minimum_required(VERSION 3.22)
project(Sofa.Component.IO.Mesh LANGUAGES CXX)

set(SOFACOMPONENTIOMESH_SOURCE_DIR "src/sofa/component/io/mesh")

set(HEADER_FILES
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/config.h.in
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/init.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BaseVTKReader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BaseVTKReader.inl
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshOBJLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshVTKLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshGmshLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VisualModelOBJExporter.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VTKExporter.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/GIDMeshLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/GridMeshCreator.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshOffLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshSTLLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshTrianLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshXspLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/OffSequenceLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/SphereLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/StringMeshCreator.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VoxelGridLoader.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BlenderExporter.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BlenderExporter.inl
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshExporter.h
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/STLExporter.h
)

set(SOURCE_FILES
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/init.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BaseVTKReader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshOBJLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshVTKLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshGmshLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VisualModelOBJExporter.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VTKExporter.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/GIDMeshLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/GridMeshCreator.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshOffLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshSTLLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshTrianLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshXspLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/OffSequenceLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/SphereLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/StringMeshCreator.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/VoxelGridLoader.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/BlenderExporter.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/MeshExporter.cpp
    ${SOFACOMPONENTIOMESH_SOURCE_DIR}/STLExporter.cpp
)

sofa_find_package(TinyXML2 REQUIRED) # BaseVTKReader
sofa_find_package(ZLIB REQUIRED) # BlenderExporter
sofa_find_package(Sofa.Simulation.Core REQUIRED)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${WRAPPER_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Simulation.Core)
target_link_libraries(${PROJECT_NAME} PRIVATE tinyxml2::tinyxml2)
target_link_libraries(${PROJECT_NAME} PUBLIC ZLIB::ZLIB)
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
    sofa_install_libraries(TARGETS ZLIB::ZLIB)
endif()

sofa_create_package_with_targets(
    PACKAGE_NAME ${PROJECT_NAME}
    PACKAGE_VERSION ${Sofa_VERSION}
    TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
    INCLUDE_SOURCE_DIR "src"
    INCLUDE_INSTALL_DIR "${PROJECT_NAME}"
)

# Tests
# If SOFA_BUILD_TESTS exists and is OFF, then these tests will be auto-disabled
cmake_dependent_option(SOFA_COMPONENT_IO_MESH_BUILD_TESTS "Compile the automatic tests" ON "SOFA_BUILD_TESTS OR NOT DEFINED SOFA_BUILD_TESTS" OFF)
if(SOFA_COMPONENT_IO_MESH_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()
